<?php

// framework init
if (version_compare(PHP_VERSION, '5.2.0', '<'))
    die('require PHP > 5.2.0 !');
defined('AROOT') or die('NO AROOT!');
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
defined('IS_DEBUG') or define('IS_DEBUG', false);
define('LP_VERSION', '4.0.0');

// 设置系统时区
if (function_exists('date_default_timezone_set'))
    date_default_timezone_set('Asia/Shanghai');

if (version_compare(PHP_VERSION, '5.4.0', '<')) {
    ini_set('magic_quotes_runtime', 0);
    define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc() ? True : False);
} else {
    define('MAGIC_QUOTES_GPC', false);
}
define('IN', true);
define('IS_CGI', substr(PHP_SAPI, 0, 3) == 'cgi' ? 1 : 0 );
define('IS_WIN', strstr(PHP_OS, 'WIN') ? 1 : 0 );
define('IS_CLI', PHP_SAPI == 'cli' ? 1 : 0);
define('REQUEST_METHOD', $_SERVER['REQUEST_METHOD']);
define('IS_GET', REQUEST_METHOD == 'GET' ? true : false);
define('IS_POST', REQUEST_METHOD == 'POST' ? true : false);
define('IS_PUT', REQUEST_METHOD == 'PUT' ? true : false);
define('IS_DELETE', REQUEST_METHOD == 'DELETE' ? true : false);
// define constant
define('PHP', '.php');
define('EXT', '.class.php');
define('EXT_FUNC', '.function.php');
define('EXT_VALID', '.validator.php');
define('ROOT', dirname(__FILE__) . DS);
define('CROOT', ROOT . 'core' . DS);
define('CLIB', CROOT . 'lib' . DS);
define('ALIB', AROOT . 'lib' . DS);
define('CCTL', CROOT . 'controller' . DS);
define('ACTL', AROOT . 'controller' . DS);
define('AVALID', AROOT . 'validator' . DS);
define('ACTLAPP', ACTL . 'app.class.php');
define('ALOG', AROOT . 'logs' . DS);
define('ACACHE', AROOT . 'cache' . DS);
//other define
define('TIME', time());
define('TODAY', date('Ymd'));
// 错误信息设置
error_reporting(E_ALL ^ E_NOTICE);
//error_reporting(E_ALL ^ E_DEPRECATED);
ini_set('display_errors', 0);
if (IS_DEBUG) {
    ini_set('display_errors', 1);
    ini_set('log_errors', 1);
    error_reporting(E_ALL);
}
ini_set('error_log', ALOG . 'error_log.txt');

// include config
require_once( CROOT . 'config' . DS . 'core.config.php' );
require_once( AROOT . 'config' . DS . (IS_DEBUG ? 'debug' : 'app') . '.config.php' );
//include core and app functions
foreach ($GLOBALS['core'] as $file) {
    if (is_file($file))
        include_once $file;
}
//增加版本号显示
define('APP_VERSION', ( IS_DEBUG ? ( C('app_version') . '_debug') : C('app_version')));
header("App-Version:" . APP_VERSION);

//init module and action
$c = $GLOBALS[C('VAR_MODULE')] = v(C('VAR_MODULE')) ? v(C('VAR_MODULE')) : C('DEFAULT_MODULE');
$a = $GLOBALS[C('VAR_ACTION')] = v(C('VAR_ACTION')) ? v(C('VAR_ACTION')) : C('DEFAULT_ACTION');
$c = basename(strtolower(z($c)));
$a = basename(strtolower(z($a)));
define('MODULE_NAME', $c);
define('ACTION_NAME', $a);

//load controller
$ctl_file = file_exists(ACTL . MODULE_NAME . EXT) ? ACTL . MODULE_NAME . EXT : (file_exists(CCTL . MODULE_NAME . EXT) ? CCTL . MODULE_NAME . EXT : FALSE);
$class_name = MODULE_NAME . 'Controller';
is_bool($ctl_file) ? die('Can\'t find controller - ' . MODULE_NAME) : require_once( $ctl_file );
class_exists($class_name) or die('Can\'t find class - ' . $class_name);
$o = new $class_name;
method_exists($o, ACTION_NAME) or die('Can\'t find method - ' . ACTION_NAME . ' ');
//allow public function
$call_uf = new ReflectionMethod($o, ACTION_NAME);
if (!$call_uf->isPublic() or $call_uf->isStatic()) {
    die('Can\'t call method - ' . ACTION_NAME . ' ');
}

//load validator
//file_exists(AVALID . MODULE_NAME . EXT_VALID) and require_once( AVALID . MODULE_NAME . EXT_VALID );
$valid_file = AVALID . MODULE_NAME . EXT_VALID;
$valid_name = MODULE_NAME . 'Validator';
if (file_exists($valid_file)) {
    require_once( $valid_file );
    if (class_exists($valid_name)) {
        $validc = new $valid_name;
        if (method_exists($validc, ACTION_NAME)) {
            $valid_result = call_user_func(array($validc, ACTION_NAME));
            $valid_result or exit();    //这里返回值的时候 如果OK就要返回return TRUE;
        }
    }
}
//call func
call_user_func(array($o, ACTION_NAME));
